package com.beauty.product.dto;

import com.beauty.product.entity.Category;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CategoryDto {
    
    private Long id;
    
    @NotBlank(message = "分类名称不能为空")
    private String name;
    
    private String description;
    
    private Long parentId;
    
    @NotNull(message = "分类层级不能为空")
    private Integer level;
    
    private Integer sort = 0;
    
    private String icon;
    
    private Boolean isShow = true;
    
    private LocalDateTime createdTime;
    
    private LocalDateTime updatedTime;
    
    private List<CategoryDto> children;
    
    public static CategoryDto fromEntity(Category category) {
        CategoryDto dto = new CategoryDto();
        dto.setId(category.getId());
        dto.setName(category.getName());
        dto.setDescription(category.getDescription());
        dto.setParentId(category.getParentId());
        dto.setLevel(category.getLevel());
        dto.setSort(category.getSort());
        dto.setIcon(category.getIcon());
        dto.setIsShow(category.getIsShow());
        dto.setCreatedTime(category.getCreatedTime());
        dto.setUpdatedTime(category.getUpdatedTime());
        return dto;
    }
    
    public Category toEntity() {
        Category category = new Category();
        category.setId(this.id);
        category.setName(this.name);
        category.setDescription(this.description);
        category.setParentId(this.parentId);
        category.setLevel(this.level);
        category.setSort(this.sort);
        category.setIcon(this.icon);
        category.setIsShow(this.isShow);
        if (this.createdTime == null) {
            category.setCreatedTime(LocalDateTime.now());
        } else {
            category.setCreatedTime(this.createdTime);
        }
        category.setUpdatedTime(LocalDateTime.now());
        return category;
    }
}